package xin.common.io;

import lombok.AllArgsConstructor;

import java.io.IOException;
import java.io.InputStream;

/**
 * <pre>
 * 输入限速流
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2019/4/26
 */
@AllArgsConstructor
public class LimitInputStream extends InputStream{

    private InputStream is;
    private BandwidthLimiter bandwidthLimiter;

    public int read() throws IOException {
        if(this.bandwidthLimiter != null)
            this.bandwidthLimiter.limitNextBytes();
        return this.is.read();
    }

    public int read(byte b[], int off, int len) throws IOException {
        if (bandwidthLimiter != null)
            bandwidthLimiter.limitNextBytes(len);
        return this.is.read(b, off, len);
    }
}
